//
//  MusicsTableViewController.swift
//  YYMusicDemo
//
//  Created by yuanyue on 2018/4/26.
//  Copyright © 2018年 yuanyue. All rights reserved.
//

private let MusicTableViewCellIdentifier = "MusicTableViewCellIdentifier"

import UIKit

class MusicsTableViewController: UITableViewController {
    
    var musics = NSMutableArray()

    override func viewDidLoad() {
        super.viewDidLoad()

        setUpUI()
        setUpData()
        self.tableView.reloadData()
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return musics.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MusicTableViewCellIdentifier, for: indexPath) as! MusicTableViewCell

        let model : MusicModel = self.musics[indexPath.row] as! MusicModel
        
        // Configure the cell...

        cell.model = model
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let model : MusicModel = musics[indexPath.row] as! MusicModel
        PlayMusicManager.shareManager.playMusic(musicModel: model)
        self.performSegue(withIdentifier: "segueMusicDetail", sender: model)
    }

    
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
//        let musicDetailVC : MusicDetailViewController = segue.destination as! MusicDetailViewController
        // Pass the selected object to the new view controller.
//        musicDetailVC.model = (sender as! MusicModel)
        
    }
    
}

extension MusicsTableViewController {
    func setUpUI() -> () {
        self.tableView.tableFooterView = UIView()
        self.tableView.register(UINib.init(nibName: "MusicTableViewCell", bundle: nil), forCellReuseIdentifier: MusicTableViewCellIdentifier)
        self.tableView.rowHeight = 60
        self.tableView.backgroundView = UIImageView.init(image: UIImage.init(named: "QQListBack.jpg"))
//        self.preferredStatusBarStyle = .lightContent

    }
        
    func setUpData() -> () {
        let path = Bundle.main.path(forResource: "Musics.plist", ofType: "")
        if (path != nil) {
            let musics = NSMutableArray()
            
            let array = NSArray(contentsOfFile: path!)
            for dict in array! {
                let model = MusicModel().dict(dict: dict as! [String : String])

                musics.add(model)
            }
            self.musics = musics
            PlayMusicManager.shareManager.musics = self.musics as! [MusicModel]
        }
    }
}







